/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2017年7月18日 下午12:03:08
 * @Copyright: 2017 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */  
package com.itttq.tools.mybatis.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**   
 * @ClassName:  FileUtil
 * @author: 迷蝶
 * @date:   2017年7月18日 下午12:03:08 
 * @Description: TODO
 *     
 * @Copyright: 2017 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
public class FileUtil {

	/**
	 * 复制文件
	 *
	 * @param source	要复制的源文件
	 * @param target	复制的目标目录
	 * @return	复制成功返回true；反之返回false；
	 * @Description: 复制文件
	 * @author: 迷蝶
	 * @return: boolean
	 * @throws
	 */
	@SuppressWarnings("resource")
	public static boolean copyFile(String source, String target) {
		boolean rs = true;	
		File file = new File(source);
		// 缓冲区大小，设置为1M
		int buffSize = 1048576;
		//得到输入通道
		FileChannel in = null;
		//得到输出通道
		FileChannel out = null;
		try {
	        in = new FileInputStream(file).getChannel();
	        out = new FileOutputStream(target).getChannel();  
	        // 设定缓冲区
	        ByteBuffer buffer = ByteBuffer.allocate(buffSize);
	        while(in.read(buffer) != -1){
	        	//准备写入，防止其他读取，锁住文件
	            buffer.flip();
	            out.write(buffer);
	            //准备读取。将缓冲区清理完毕，移动文件内部指针
	            buffer.clear();
	        }
		} catch (IOException e) {
			e.printStackTrace();
			rs = false;
		} finally {
			try {
				if(null != out) out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if(null != in) in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return rs;
	}
}
